package com.dingqb.mycs.ui.res;

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.VideoView;

import com.base.BaseActivity;
import com.dingqb.mycs.R;

public class AssetRawActivity extends BaseActivity implements OnClickListener {

	TextView tv_asset_audio;
	TextView tv_asset_video;
	TextView tv_raw_video;
	TextView tv_raw_audio;
	VideoView videoView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_asset);
		tv_asset_audio = (TextView) findViewById(R.id.tv_asset_audio);
		tv_asset_video = (TextView) findViewById(R.id.tv_asset_video);
		tv_raw_video = (TextView) findViewById(R.id.tv_raw__video);
		tv_raw_audio = (TextView) findViewById(R.id.tv_raw_audio);
		videoView = (VideoView) findViewById(R.id.videoView);

		tv_asset_audio.setOnClickListener(this);
		tv_asset_video.setOnClickListener(this);
		tv_raw_video.setOnClickListener(this);
		tv_raw_audio.setOnClickListener(this);

	}

	private void AssetAudio() {
		// asset
		try {
			AssetManager assetManager = this.getAssets();
			AssetFileDescriptor afd = assetManager.openFd("hongbao_gq.mp3");
			MediaPlayer player = new MediaPlayer();
			player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
			player.setLooping(true);// 循环播放
			player.prepare();
			player.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void rawVideo() {
		// raw音频
		Uri notification = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.hongbao_gq);
		Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
		r.play();
	}

	private void rawAudio() {
		// raw视频
		String uri = "android.resource://" + getPackageName() + "/" + R.raw.hongbao_gx;
		videoView.setVideoURI(Uri.parse(uri));
		videoView.start();
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.tv_asset_audio:
			AssetAudio();
			break;
		case R.id.tv_asset_video:
			AssetAudio();
			break;

		case R.id.tv_raw_audio:
			rawAudio();
			break;
		case R.id.tv_raw__video:
			rawVideo();
			break;
		default:
			break;
		}

	}

}
